package com.wm.blog_web.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_common.base.BeanUtils;
import com.wm.blog_common.bean.PageHelper;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TWebConfigDO;
import com.wm.blog_common.entity.TWebConfig;
import com.wm.blog_common.enums.DeleteFlag;
import com.wm.blog_common.result.Result;
import com.wm.blog_web.dao.TWebConfigDAO;
import com.wm.blog_web.query.TWebConfigQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wm_yu
 * @since 2020-03-24
 */
@Service
public class TWebConfigService {
    @Autowired
    private TWebConfigDAO tWebConfigDAO;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Result<TWebConfigDO> selectById(Long id){
        return Result.success(BeanUtils.copyProperties(tWebConfigDAO.getById(id),TWebConfigDO.class));
    }

    /**
    * 根据ID删除
    * @param id
    * @return
    */
    public Result deleteById(Long id){
        return tWebConfigDAO.removeById(id)?Result.success():Result.failure();
    }

    /**
    * 添加
    * @param tWebConfig
    * @return
    */
    public Result insert(TWebConfig tWebConfig){
        return tWebConfigDAO.save(tWebConfig)?Result.success():Result.failure();
    }

    /**
    * 修改
    * @param tWebConfig
    * @return
    */
    public Result update(TWebConfig tWebConfig){
        return tWebConfigDAO.updateById(tWebConfig)?Result.success():Result.failure();
    }

    /**
    * 统计总数
    *
    * @param query
    * @return
    */
    public Result<Integer> count(TWebConfigQuery query) {
        return Result.success(tWebConfigDAO.count(this.combineQuery(query)));
    }

    /**
    * 列表查询
    * @param query
    * @return
    */
    public Result<List<TWebConfigDO>> list(TWebConfigQuery query){
        return Result.success(BeanUtils.copyListProperties(tWebConfigDAO.list(this.combineQuery(query)),TWebConfigDO.class));
    }
    /**
    * 分页查询
    * @param page
    * @return
    */
    public Result<Page<TWebConfigDO>> page(QueryPage page, TWebConfigQuery query){
        return Result.success(PageHelper.newPage(tWebConfigDAO.page(page,this.combineQuery(query)),TWebConfigDO.class));
    }

    /**
    * 组装查询条件
    *
    * @param query
    * @return
    */
    private QueryWrapper<TWebConfig> combineQuery(TWebConfigQuery query) {
        QueryWrapper<TWebConfig> wrapper = new QueryWrapper<>();
        Optional.ofNullable(query).ifPresent(item -> {
            Optional.ofNullable(item.getStatus()).ifPresent(x -> {
                wrapper.eq("status",x);
            });
        });
        return wrapper;
    }

    /**
     * 查询当前正在启用的网站配置
     * @return
     */
    public Result<TWebConfigDO> getConfig() {
        TWebConfigQuery query = new TWebConfigQuery();
        query.setStatus(DeleteFlag.ENABLE.getValue());
        QueryWrapper<TWebConfig> wrapper = this.combineQuery(query);
        wrapper.last("limit 1");
        TWebConfig webConfig = Optional.ofNullable(tWebConfigDAO.getOne(wrapper)).orElse(new TWebConfig());
        return Result.success(BeanUtils.copyProperties(webConfig,TWebConfigDO.class));
    }
}







